// *******************************************************************************************************
// # エリア
//   ・検索結果 サイドナビゲーション
// *******************************************************************************************************

var ResultBridge = ResultBridge || {};

$(function() {
	var isUnderIe6 = uaInfo.isIe && uaInfo.ieVr <= 6,
		isUnderIe7 = uaInfo.isIe && uaInfo.ieVr <= 7,
		isUnderIe8 = uaInfo.isIe && uaInfo.ieVr <= 8;

	/*******************************************************
	 *　サイドナビ
	 *******************************************************/
	var $navi = $('#contentSubColumn');

	function toggoleCheckReload($title, $content){
		var b = false;
		if ($title.hasClass('active')) {
			$content.show();
			b = true;
		} else {
			$content.hide();
			b = false;
		};
		toggleChangeActive($title, $content, b);
	};

	function toggleChangeActive($title, $content, b){
		if (b) {
			$title.addClass('active');
			$title.find('a span').removeClass('icn_plus');
			$title.find('a span').addClass('icn_minus');
			$title.find('a span').text('閉じる');
		} else {
			$title.removeClass('active');
			$title.find('a span').removeClass('icn_minus');
			$title.find('a span').addClass('icn_plus');
			$title.find('a span').text('開く');
		};
	};

	/*******************************************************/
	// 地域
	$navi.find('.mod_box_sub.area .mod_box_area_child').each(function(i){
		if ($(this).find('a')[0]) {
			var $title   = $(this).find('.box_ttl_child'),
				$content = $(this).find('.list_arealine');
			var tgl = new APMN.ToggleOpener({
					btn: $title,
					target: $content,
					isOpen: $title.hasClass('active')
			});
			tgl.toggle = function(b) {
				toggleChangeActive($title, $content, b);
			};
			tgl.moving = function(b) {
				appendWrapper.changeCtsHeight();
			};
			toggoleCheckReload($title, $content);
		};
	});

	/*******************************************************/
	// 地域 追加・削除
	function areaModalSetting(){
		var $btn = $navi.find('.mod_box_sub.area .list_btn_action .left a');

		$btn.on('click', function(e){
			e.preventDefault();
			$('html,body').stop().animate({ scrollTop: 0 }, 1 );
			show();

			//検索されている市区町村
			var cityList = new Array();
			$(".search_target").each(function(){
				cityList.push($(this).attr('id'));
			});

			$modal.find('.mod_check').each(function(e){
				if($.inArray($(this).attr('id'), cityList) != -1){
					if (!isUnderIe8) {
						$(this).find('input').iCheck('check');
					}
					else {
						$(this).find('input').prop('checked', true);
					}
					change();
				}
			});
			//検索されている市区町村
		});

		// モーダル
		var $modal = $('#modal_result_area'),
			modal = null,
			defInput = [],
			dispatch = true;

		modal = new APMN.Modal({
			target: $modal,
			close:  $modal.find('.modal_close'),
			ieVr: !(isUnderIe6),
			nonAppend: true
		});
		function init(){
			$modal.find('.mod_btn_append a').on('click', function(e){
				//
			});
			$modal.find('.btn_close a').on('click', function(e){
				e.preventDefault();
				hide();
			});


			$modal.find('.box_category').each(function(i){
				var $self = $(this);
				if (!isUnderIe7) {
					$self.find('.mod_ttl_bar input').on('ifChecked', function(e){
						if (dispatch) return;
						dispatch = true;
						$self.find('li input').iCheck('check');
						change();
					}).on('ifUnchecked', function(e){
						if (dispatch) return;
						dispatch = true;
						$self.find('li input').iCheck('uncheck');
						change();
					});
					$self.find('li input').each(function(j){
						$(this).on('ifChecked', function(e){
							if (dispatch) return;
							dispatch = true;
							change();
						}).on('ifUnchecked', function(e){
							if (dispatch) return;
							dispatch = true;
							if ($self.find('.mod_ttl_bar.mod_check').hasClass('mod_checked')) {
								$self.find('.mod_ttl_bar input').iCheck('uncheck');
							};
							change();
						});
					});
				} else {
					$self.find('.mod_ttl_bar input').on('click', function(e){
						var b = $(this).prop('checked');
						$self.find('li input').prop('checked', b);
						change();
					});
					$self.find('li input').each(function(j){
						$(this).on('click', function(e){
							var b = $(this).prop('checked');
							if (!b) {
								$self.find('.mod_ttl_bar input').prop('checked', false);
							};
							change();
						});
					});
				};
			});
			$modal.find('.mod_check').each(function(e){
				defInput.push($(this).hasClass('mod_checked'));
			});
			change();
		};
		function change(){
			dispatch = false;
			//再検索ボタン
			if (!isUnderIe8) {
				if($modal.find('.mod_checked').length > 0){
					$modal.find('.mod_btn_all').removeClass('mod_btn_disabled');
				}
				else {
					$modal.find('.mod_btn_all').addClass('mod_btn_disabled');
				}
			}
			else {
				if($modal.find('li input:checked').length > 0){
					$modal.find('.mod_btn_all').removeClass('mod_btn_disabled');
				}
				else {
					$modal.find('.mod_btn_all').addClass('mod_btn_disabled');
				}
			}
		};
		function show(){
			$modal.find('input').each(function(i){
				if (!isUnderIe8) {
					if (defInput[i]) $(this).iCheck('check');
					else $(this).iCheck('uncheck');
				} else {
					$(this).prop('checked', defInput[i]);
				};
			});
			modal.show();
		};
		function hide(){
			modal.hide();
		};
		init();
	};
	areaModalSetting();
	/*******************************************************/
	// 路線・駅
	$navi.find('.mod_box_sub.line .mod_box_area_child').each(function(i){
		if ($(this).find('a')[0]) {
			var $title   = $(this).find('.box_ttl_child'),
				$content = $(this).find('.list_arealine');
			var tgl = new APMN.ToggleOpener({
					btn: $title,
					target: $content,
					isOpen: $title.hasClass('active')
			});
			tgl.toggle = function(b) {
				toggleChangeActive($title, $content, b);
			};
			tgl.moving = function(b) {
				appendWrapper.changeCtsHeight();
			};
			toggoleCheckReload($title, $content);
		};
	});

	/*******************************************************/
	// 路線・駅 路線を変更
	function lineModalLineSetting(){
		var $btn = $navi.find('.mod_box_sub.line .list_btn_action .left a');

		$btn.on('click', function(e){
			e.preventDefault();
			$('html,body').stop().animate({ scrollTop: 0 }, 1 );
			show();
		});

		// モーダル
		var $modal = $('#modal_result_line'),
			modal = null;

		modal = new APMN.Modal({
			target: $modal,
			close:  $modal.find('.modal_close'),
			ieVr: !(isUnderIe6)
		});
		function init(){
			$modal.find('.btn_close a').on('click', function(e){
				e.preventDefault();
				hide();
			});
		};
		function show(){
			modal.show();
		};
		function hide(){
			modal.hide();
		};
		init();
	};
	lineModalLineSetting();

	/*******************************************************/
	// 路線・駅 駅を変更
	function lineModalStationSetting(){
		var $btn = $navi.find('.mod_box_sub.line .list_btn_action .right a');

		$btn.on('click', function(e){
			e.preventDefault();
			$('html,body').stop().animate({ scrollTop: 0 }, 1 );
			show();
			//検索されている駅
			var ekiList = new Array();
			$(".search_target").each(function(){
				ekiList.push($(this).attr('id'));
			});
			$modal.find('.mod_check').each(function(e){
				if($.inArray($(this).attr('id'), ekiList) != -1){
					if (!isUnderIe8) {
						$(this).find('input').iCheck('check');
					}
					else {
						$(this).find('input').prop('checked', true);
					}
					change();
				}
			});
		});

		// モーダル
		var $modal = $('#modal_result_station'),
			modal = null,
			defInput = [],
			dispatch = true;

		modal = new APMN.Modal({
			target: $modal,
			close:  $modal.find('.modal_close'),
			ieVr: !(isUnderIe6),
			nonAppend: true
		});
		function init(){
			$modal.find('.mod_btn_append a').on('click', function(e){
				//
			});
			$modal.find('.btn_close a').on('click', function(e){
				e.preventDefault();
				hide();
			});
			$modal.find('.box_category').each(function(i){
				var $self = $(this);
				if (!isUnderIe7) {
					$self.find('.mod_ttl_bar input').on('ifChecked', function(e){
						if (dispatch) return;
						dispatch = true;
						$self.find('li input').iCheck('check');
						change();
					}).on('ifUnchecked', function(e){
						if (dispatch) return;
						dispatch = true;
						$self.find('li input').iCheck('uncheck');
						change();
					});
					$self.find('li input').each(function(j){
						$(this).on('ifChecked', function(e){
							if (dispatch) return;
							dispatch = true;
							change();
						}).on('ifUnchecked', function(e){
							if (dispatch) return;
							dispatch = true;
							if ($self.find('.mod_ttl_bar.mod_check').hasClass('mod_checked')) {
								$self.find('.mod_ttl_bar input').iCheck('uncheck');
							};
							change();
						});
					});
				} else {
					$self.find('.mod_ttl_bar input').on('click', function(e){
						var b = $(this).prop('checked');
						$self.find('li input').prop('checked', b);
						change();
					});
					$self.find('li input').each(function(j){
						$(this).on('click', function(e){
							var b = $(this).prop('checked');
							if (!b) {
								$self.find('.mod_ttl_bar input').prop('checked', false);
							};
							change();
						});
					});
				};
			});
			$modal.find('.mod_check').each(function(e){
				defInput.push($(this).hasClass('mod_checked'));
			});
			change();
		};
		function change(){
			dispatch = false;

			//再検索ボタン
			if (!isUnderIe8) {
				if($modal.find('.mod_checked').length > 0){
					$modal.find('.mod_btn_all').removeClass('mod_btn_disabled');
				}
				else {
					$modal.find('.mod_btn_all').addClass('mod_btn_disabled');
				}
			}
			else {
				if($modal.find('li input:checked').length > 0){
					$modal.find('.mod_btn_all').removeClass('mod_btn_disabled');
				}
				else {
					$modal.find('.mod_btn_all').addClass('mod_btn_disabled');
				}
			}
		};
		function show(){
			$modal.find('input').each(function(i){
				if (!isUnderIe8) {
					if (defInput[i]) $(this).iCheck('check');
					else $(this).iCheck('uncheck');
				} else {
					$(this).prop('checked', defInput[i]);
				};
			});
			modal.show();
		};
		function hide(){
			modal.hide();
		};
		init();
	};
	lineModalStationSetting();

	/*******************************************************/
	//マップ駅選択
	function mapModalStationSetting(){

		var $btn = $('.map a');
		var type = $('input[name=search_type]').val();
		var mapCount = $('[name=map_target]').length;


		$btn.on('click', function(e){
			e.preventDefault();
			$('html,body').stop().animate({ scrollTop: 0 }, 1 );
			if(type == 'shop'){ //ショップ
				ShowType(2, $('.search_target').attr('id'));
			}
			else {
				//路線・駅、市区町村選択一つ→マップ表示
				if(mapCount <= 1){
					ShowType(2, $('.search_target').attr('id'));
				}
				else { //路線・駅、市区町村選択
					show();
				}
			}
		});

		//リンククリック→マップ表示
		$('[name=map_target]').on('click', function(){
			ShowType(2, $(this).attr('id'));
		});

		// モーダル
		var modalId = (type == 'line' ? 'modal_map_station' : 'modal_map_area')
		var $modal = $('#' + modalId),
			modal = null,
			defInput = [],
			dispatch = true;

		modal = new APMN.Modal({
			target: $modal,
			close:  $modal.find('.modal_close'),
			ieVr: !(isUnderIe6),
			nonAppend: true
		});
		function init(){
			$modal.find('.mod_btn_append a').on('click', function(e){
				//
			});
			$modal.find('.btn_close a').on('click', function(e){
				e.preventDefault();
				hide();
			});
			hide();
		};
		function show(){
			modal.show();
		};
		function hide(){
			modal.hide();
		};
		init();
	}


	/*******************************************************/
	// 基本条件
	$navi.find('.mod_box_sub.basic .mod_box_sub_child').each(function(i){
		if ($(this).find('a')[0]) {
			var $title   = $(this).find('.box_title'),
				$content = $(this).find('.box_content');
			var tgl = new APMN.ToggleOpener({
					btn: $title,
					target: $content,
					isOpen: $title.hasClass('active')
			});
			tgl.toggle = function(b) {
				toggleChangeActive($title, $content, b);
			};
			tgl.moving = function(b) {
				appendWrapper.changeCtsHeight();
			};
			toggoleCheckReload($title, $content);
		};
	});

	// 賃料
	var rentSelecter = (function(){
		var that = {
			$target: $navi.find('.mod_box_sub.basic .mod_box_sub_child.rent ul.list_select'),
			$down: $navi.find('.mod_box_sub.basic .mod_box_sub_child.rent ul.list_select').find('li').eq(0),
			$up:   $navi.find('.mod_box_sub.basic .mod_box_sub_child.rent ul.list_select').find('li').eq(1),
			dwVal: 0,
			upVal: 9999999
		};
		function init(){
			that.dwVal = parseInt(that.$down.find('select').val());
			that.upVal = parseInt(that.$up.find('select').val());
			that.$down.on('change', function(e){
				that.dwVal = parseInt($(this).find('select').val());
				if (that.isChanging) return;
				check();
			});
			that.$up.on('change', function(e){
				that.upVal = parseInt($(this).find('select').val());
				if (that.isChanging) return;
				check();
			});
		};
		function check(){
			that.isChanging = true;
			if (that.dwVal != 0 && that.upVal != 0 && that.dwVal > that.upVal) {
				var _val1 = that.upVal;
				var _val2 = that.dwVal;
				that.dwVal = _val1;
				that.upVal = _val2;
				that.$down.find('select').val(_val1);
				that.$up.find('select').val(_val2);
				that.$down.find('.custom_selectInner').text(toSelectText(_val1));
				that.$up.find('.custom_selectInner').text(toSelectText(_val2));
			};
			that.isChanging = false;
		};
		function toSelectText(str){
			return str * 0.0001 + '万円';
		}
		init();
		return that;
	}());

	/*******************************************************/
	// 基本条件 間取り
	function basicMadoriSetting(){
		var $box = $('.mod_box_sub.basic .mod_box_sub_child.madori'),
			$type = $box.find('.list_btn_type li'),
			$typeBox = $box.find('input[name=room_type]'),
			$room = $box.find('.list_checkbox2 li'),
			$roomBtn = $box.find('.box_append li'),
			data = {
				single: ['ワンルーム','1K','1DK','1LDK','2K','2DK'],
				double: ['1LDK','2K','2DK','2LDK','3K','3DK','3LDK'],
				family: ['2LDK','3K','3DK','3LDK','4K','4DK','4LDK','5K以上']
			},
			id = $typeBox.val(),
			isCheck = null;

		$type.each(function(i){
			var _id = $(this).attr('class');
			$(this).on('click', function(e){
				id = _id;
				$typeBox.val(id);
				changeType();
				changeRoom();
				checkUnder();
			});
		});
		$room.each(function(i){
			if (!isUnderIe8) {
				$(this).find('input').on('ifChecked', function(e){
					checkUnder();
				}).on('ifUnchecked', function(e){
					checkUnder();
				});
			} else {
				$(this).find('input').on('click', function(e){
					checkUnder();
				});
			};
		});

		function changeType(){
			$type.each(function(i){
				if ($(this).attr('class') == id) {
					$(this).find('a').addClass('active');
				} else {
					$(this).find('a').removeClass('active');
				};
			});
		};
		function changeRoom(){
			$room.each(function(i){
				var $self = $(this).find('input'),
					_id = $self.val(),
					_on = false,
					_len = data[id].length;
				for (var j = 0; j < _len; j++) {
					if (data[id][j] == _id) {
						_on = true;
						break;
					};
				};
				if (!isUnderIe8) {
					if (_on) {
						$self.iCheck('check');
					} else {
						$self.iCheck('uncheck');
					};
				} else {
					$self.prop('checked', _on);
				};
			});
		};
		function checkUnder(){
			if (isCheck) return;
			var _id = null;
			setTimeout(function(e){
				$room.each(function(i){
					var $self = $(this).find('input');
					if ($self.prop('checked')) {
						_id = $self.val();
						return false;
					};
				});
				if (_id) {
					$roomBtn.find('span').html(_id + 'より<br>大きい間取りを<br>すべて検索');
					$('#madoriGroup').val(_id);
					$roomBtn.removeClass('mod_btn_disabled');
					if (!isUnderIe8) {
						$roomBtn.find('input').iCheck('enable');
					} else {
						$roomBtn.find('input').prop('disabled', false);
					};
				} else {
					$('#madoriGroup').val('');
					$roomBtn.find('span').html('間取りを選択');
					$roomBtn.addClass('mod_btn_disabled');
					$roomBtn.removeClass('mod_checked');
					if (!isUnderIe8) {
						$roomBtn.find('input').iCheck('uncheck');
						$roomBtn.find('input').iCheck('disable');
					} else {
						$roomBtn.find('input').prop('checked', false);
						$roomBtn.find('input').prop('disabled', true);
					};
				};
				isCheck = null;
			}, 1);
		};

		if ($roomBtn.find('input').prop('checked')) {
			$roomBtn.addClass('mod_checked');
		};
		/*
		if (id=='' || !id) {
			id = 'double';
			$typeBox.val(id);
			changeRoom();
		};*/
		changeType();
		checkUnder();
	};
	basicMadoriSetting();

	// 専有面積
	var exclusiveSelecter = (function(){
		var that = {
			$target: $navi.find('.mod_box_sub.basic .mod_box_sub_child.exclusive ul.list_select'),
			$down: $navi.find('.mod_box_sub.basic .mod_box_sub_child.exclusive ul.list_select').find('li').eq(0),
			$up:   $navi.find('.mod_box_sub.basic .mod_box_sub_child.exclusive ul.list_select').find('li').eq(1),
			dwVal: 0,
			upVal: 99999
		};
		function init(){
			that.dwVal = parseInt(that.$down.find('select').val());
			that.upVal = parseInt(that.$up.find('select').val());
			that.$down.on('change', function(e){
				that.dwVal = parseInt($(this).find('select').val());
				if (that.isChanging) return;
				check();
			});
			that.$up.on('change', function(e){
				that.upVal = parseInt($(this).find('select').val());
				if (that.isChanging) return;
				check();
			});
		};
		function check(){
			that.isChanging = true;
			if (that.dwVal != 0 && that.upVal != 0 && that.dwVal > that.upVal) {
				var _val1 = that.upVal;
				var _val2 = that.dwVal;
				that.dwVal = _val1;
				that.upVal = _val2;
				that.$down.find('select').val(_val1);
				that.$up.find('select').val(_val2);
				that.$down.find('.custom_selectInner').html(toSelectText(_val1));
				that.$up.find('.custom_selectInner').html(toSelectText(_val2));
			};
			that.isChanging = false;
		};
		function toSelectText(str){
			return str + 'm&sup2;';
		}
		init();
		return that;
	}());


	/*******************************************************/
	// 絞り込み条件
	$navi.find('.mod_box_sub.narrow .mod_box_sub_child').each(function(i){
		if ($(this).find('a')[0]) {
			var $title   = $(this).find('.box_title'),
				$content = $(this).find('.box_content');
			var tgl = new APMN.ToggleOpener({
					btn: $title,
					target: $content,
					isOpen: $title.hasClass('active')
			});
			tgl.toggle = function(b) {
				toggleChangeActive($title, $content, b);
			};
			tgl.moving = function(b) {
				appendWrapper.changeCtsHeight();
			};
			toggoleCheckReload($title, $content);
		};
	});

	/*******************************************************/
	// 親
	$navi.find('.mod_box_sub').each(function(i){
		$(this).find('.box_title').first().not('.non').each(function(j){
			var $title   = $(this),
				$content = $(this).next();
			var tgl = new APMN.ToggleOpener({
					btn: $title,
					target: $content,
					isOpen: $title.hasClass('active')
				});
			tgl.toggle = function(b) {
				toggleChangeActive($title, $content, b);
			};
			tgl.moving = function(b) {
				appendWrapper.changeCtsHeight();
			};
			toggoleCheckReload($title, $content);
		});
	});


	/*******************************************************
	 *　再検索
	 *******************************************************/
	$('#appendContainerWrapper .mod_btn_research a').removeClass('mod_btn_disabled');
	$('#contentSubColumn .btn_research a').removeClass('mod_btn_disabled');

	/*******************************************************
	 *　まとめて問い合わせラッパー
	 *******************************************************/
	var appendWrapper = new APMN.AppendWrapper({
		isFixed: true,
		ieVr: !(isUnderIe6)
	});

	// bridge
	ResultBridge.appendChange = function(){
		appendController.change();
	};

	var appendController = (function(){
		var that ={
		};
		that.change = function(){
			appendWrapper.changeCtsHeight();
			if (ResultBridge.callBackCtsHeightEvent) {
				ResultBridge.callBackCtsHeightEvent();
			};
		};
		return that;
	})();


	//クッキー用の保存
	function saveCondition(){
		var items=[];
		var itemsName=[];
		var typ = $('input[name="search_type"]').attr("value");
		var listtype = $('#listtype').val();
		items.push('search_type:' + typ);
		items.push('listtype:' + listtype);

		$('.search_target').each(function(index){
			if($(this).attr("id") != ""){
				items.push('target_'+index + ':' + $(this).attr("id"));
			}
		});

		//町名検索
		$('li[name=search_targetChou]').each(function(index){
			items.push('targetchou_'+index + ':' + $(this).attr("id"));
		});

		//グループ
		if($('#madoriGroupCheck').attr('checked')){
			var firstIndex = -1;
			var firstMadori = $('#madoriGroup').val();
			$('input[type=checkbox][name^=madori]').each(function(index){
				if($(this).val() == firstMadori){
					firstIndex = index;
					return;
				}
			});

			$('input[type=checkbox][name^=madori]').each(function(index){
				if(index >= firstIndex && $(this).attr('id') != 'madoriGroup'){
					$(this).attr('checked', true);
				}
			});
		}
		//グループ

		$('input:checked').each(function(index){
			var elem = $(this);
			var val = elem.attr("value");
			if(elem.attr('name').indexOf('madori') == 0){
				val = 'on';
			}
			if(elem.attr("name") != ""){
				items.push(elem.attr("name") + ':' + val);
			}
		});

		$('select').each(function(index){
			if($(this).attr('name').indexOf('this_params') >= 0) return;
			var elem = $(this);
			if(elem.attr("name") != ""){
				var val = elem.find("option:selected").attr("value");
				var name = elem.attr("name");
				items.push(elem.attr("name") + ':' + val);
			}
		});


		//名所
		var itemsName = [];
		var mainData = [];
		//地域検索
		if(typ == 'area'){
			var pref = $('.ttl_arealine').html();

			//市区町村
			$('.search_target').each(function(){
				var val = pref + '　' + $(this).html();
				var city = $(this).attr('id').split('-')[1];

				//町
				var childCount = $('li[id^=' + city + ']').length;
				if(childCount > 0) val += '　';
				$('li[id^=' + city + ']').each(function(index){
					val += $(this).html();
					if(index < childCount - 1){
						val += '、';
					}
				})

				mainData.push(val);
			}) ;
		}
		//路線検索
		else if(typ == 'line'){
			var prefName = $('#liPrefname').html();
			var pref = $('.search_target').first().attr('id').split('-')[0];
			$('.ttl_area_child').each(function(){
				var ensen = pref + '-' + $(this).attr('id');
				var val = prefName + '　' + $(this).html();

				var length = $('.search_target[id^=' + ensen + ']').length;
				if(length > 0) val += '　';
				$('.search_target[id^=' + ensen + ']').each(function(index){
					val += $(this).html();
					if(index < length - 1){
						val += '、';
					}
				})
				mainData.push(val);
			});
		}
		//ショップ検索
		else if(typ == 'shop'){
			mainData.push($('.mod_link_arrow_blue').html());
		}

		//メインデータ
		itemsName.push({mainData: mainData});

		//賃料
		var tinryo = '';
		var index1 = $('select[name=tinryo1]').find('option:selected').index();
		var index2 = $('select[name=tinryo2]').find('option:selected').index();
		var length = $('select[name=tinryo2] option').length;
		if(index1 > 0 && index2 < length - 1){
			tinryo = $('select[name=tinryo1]').find('option:selected').text() + '～' + $('select[name=tinryo2]').find('option:selected').text();
		}
		else if(index1 > 0) tinryo = $('select[name=tinryo1]').find('option:selected').text() + '～';
		else if(index2 < length - 1) tinryo = '～' + $('select[name=tinryo2]').find('option:selected').text();
		if(index1 > 0 || index2 < length - 1){
			var vals = [];
			if($('#tinryo_jouken2').attr('checked')) vals.push('管理費・共益費含む');
			if($('#tyusyajyo_komi').attr('checked')) vals.push('駐車場代含む');
			if($('#shikikin_nashi').attr('checked')) vals.push('敷金ゼロ');
			if($('#reikin_nashi').attr('checked')) vals.push('礼金ゼロ');
			if($('#freerent').attr('checked')) vals.push('フリーレント');
			if($('#heyawari').attr('checked')) vals.push('部屋割');
			if(vals.length > 0){
				tinryo += '（' + vals.join('、') + '）';
			}
		}
		itemsName.push({tinryo:tinryo});
		//賃料

		//間取り
		var madoriMst = {
				madori10:'ワンルーム',
				madori11:'1K',
				madori12:'1DK',
				madori14:'1LDK',
				madori21:'2K',
				madori22:'2DK',
				madori24:'2LDK',
				madori31:'3K',
				madori32:'3DK',
				madori34:'3LDK',
				madori41:'4K',
				madori42:'4DK',
				madori44:'4LDK',
				madori51:'5K以上'
		};
		var madori = [];
		$('input[type=checkbox][id^=madori]:checked').each(function(){
			madori.push(madoriMst[$(this).attr('id')]);
		});
		itemsName.push({madori:madori.join('、')});
		//間取り

		//面積
		var senyu = '';
		var tinryoIndex1 = $('select[name=senyu1]').find('option:selected').index();
		var tinryoIndex2 = $('select[name=senyu2]').find('option:selected').index();
		var length = $('select[name=senyu1] option').length;
		if(tinryoIndex1 > 0 && tinryoIndex2 < length - 1){
			senyu = $('select[name=senyu1]').find('option:selected').text() + '～' + $('select[name=senyu2]').find('option:selected').text();
		}
		itemsName.push({senyu:senyu});
		//面積

		//物件種別
		var syubetsu = [];
		$('input[type=checkbox][name^=syubetu]:checked').each(function(){
			var name = $(this).attr('name');
			if(name == 'syubetu1') syubetsu.push('賃貸アパート');
			else if(name == 'syubetu2') syubetsu.push('賃貸マンション');
			else if(name == 'syubetu3') syubetsu.push('戸建て・テラスハウス');
			else if(name == 'syubetu4') syubetsu.push('貸店舗・賃貸事務所');
			else if(name == 'syubetu5') syubetsu.push('駐車場');
		});
		itemsName.push({syubetu:syubetsu.join('、')});
		//物件種別

		//築年数
		var build = '';
		if($('select[name=built]').find('option:selected').index() > 0){
			build = $('select[name=built]').find('option:selected').text();
		}
		itemsName.push({build:build});
		//築年数

		//駅徒歩
		var ekitoho = '';
		if($('select[name=ekitoho]').find('option:selected').index() > 0){
			ekitoho = $('select[name=ekitoho]').find('option:selected').text();
			if($('#bas_riyou').attr('checked')) ekitoho += '（バス利用可）';
		}
		itemsName.push({ekitoho:ekitoho});
		//駅徒歩

		//その他
		var other = [];
		$('input[type=checkbox][name^=kodawari]:checked').each(function(){
			var div = $(this).parent().first();
			var label = div.parent().first();
			var text = label.contents().filter(function() {
				return this.nodeType == 3;
			});
			other.push(text[text.length - 1].data);
		});
		$('input[type=checkbox][name^=campaign]:checked').each(function(){
			var div = $(this).parent().first();
			var label = div.parent().first();
			var text = label.contents().filter(function() {
				return this.nodeType == 3;
			});
			other.push(text[text.length - 1].data);
		});
		itemsName.push({other:other.join('/')});
		//その他

		//保存タイム
		var d = new Date();
		var date = d.getDate();
		var month = d.getMonth() + 1;
		var hour = d.getHours();
		var minute = d.getMinutes();
		var savetime = (month < 10 ? '0' : '') + String(month) + '/' + (date < 10 ? '0' : '') + String(date) + ' ' +
					   (hour < 10 ? '0' : '') + String(hour) + ':' + (minute < 10 ? '0' : '') + String(minute);
		itemsName.push({savetime:savetime});


		CONS.addCondition(items, itemsName);
	}

	/*******************************************************
	 *　この条件を保存
	 *******************************************************/
	function saveSetting(){
		$navi.find('.btn_save a').on('click', function(e){
			addSave.show();

			//条件保存
			saveCondition();
		});

		var addSave = new APMN.ModalStar({
			path: '/static/area/images/result/img_add_conditions.png',
			ieVr: !(isUnderIe6),
			type: 2
		});
	};

	//クッキー用の保存
	function saveInquiryCondition(href){

		var items=[];
		var itemsName=[];
		var typ = $('input[name="search_type"]').attr("value");
		var listtype = $('#listtype').val();

		if(typ == 'area') items.push({name:'kind', value:'1'});
		else if(typ == 'line') items.push({name:'kind', value:'2'});
		else if(typ == 'shop') items.push({name:'kind', value:'3'});

		//items.push({name:'kind', value:typ});

		var name = [];
		//地域検索
		if(typ == 'area'){
			var pref = $('.ttl_arealine').html();

			//市区町村
			$('.search_target').each(function(){
				var val = pref + '　' + $(this).html();
				var city = $(this).attr('id').split('-')[1];

				//町
				var childCount = $('li[id^=' + city + ']').length;
				if(childCount > 0) val += ';';
				$('li[id^=' + city + ']').each(function(index){
					val += $(this).html();
					if(index < childCount - 1){
						val += '、';
					}
				})

				name.push(val);
			}) ;
		}
		//路線検索
		else if(typ == 'line'){
			var prefName = $('#liPrefname').html();

			var pref = $('.search_target').first().attr('id').split('-')[0];
			$('.ttl_area_child').each(function(){
				var ensen = pref + '-' + $(this).attr('id');

				var val = prefName + '　' + $(this).html();

				var length = $('.search_target[id^=' + ensen + ']').length;
				if(length > 0) val += ';';
				$('.search_target[id^=' + ensen + ']').each(function(index){
					val += $(this).html();
					if(index < length - 1){
						val += '、';
					}
				})
				name.push(val);
			});
		}
		//ショップ検索
		else if(typ == 'shop'){
			name.push($('.mod_link_arrow_blue').html());
		}

		//メインデータ
		items.push({name:'name',value:name.join('/')});

		var code = [];
		$('.search_target').each(function(index){
			if($(this).attr("id") != ""){
				var id = $(this).attr("id").split('-')[1];
				code.push(id);
			}
		});
		//町名検索
		$('li[name=search_targetChou]').each(function(index){
			var id = $(this).attr("id");
			code.push(id);
		});

		items.push({name:'code', value:code.join(',')});

		//賃料
		var tinryo1 = $('select[name=tinryo1]').val();
		items.push({name:'tinryo1', value:(tinryo1/10000)});

		var tinryo2 = $('select[name=tinryo2]').val();
		if(tinryo2 == '9999999') tinryo2 = '9990000'
		items.push({name:'tinryo2', value:(tinryo2/10000)});

		var tinryo_jouken2 = $('#tinryo_jouken2').attr('checked') ? '1' : '0';
		items.push({name:'tinryo_jouken2', value:tinryo_jouken2});

		var tyusyajyo_komi = $('#tyusyajyo_komi').attr('checked') ? '1' : '0';
		items.push({name:'tyusyajyo_komi', value:tyusyajyo_komi});

		var shikikin_nashi = $('#shikikin_nashi').attr('checked') ? '1' : '0';
		items.push({name:'shikikin_nashi', value:tyusyajyo_komi});

		var reikin_nashi = $('#reikin_nashi').attr('checked') ? '1' : '0';
		items.push({name:'reikin_nashi', value:reikin_nashi});

		var freerent = $('#freerent').attr('checked') ? '1' : '0';
		items.push({name:'freerent', value:freerent});

		var heyawari = $('#heyawari').attr('checked') ? '1' : '0';
		items.push({name:'heyawari', value:heyawari});

		var syubetu1 = $('#syubetu1').attr('checked') ? '1' : '0';
		items.push({name:'syubetu1', value:syubetu1});
		var syubetu2 = $('#syubetu2').attr('checked') ? '1' : '0';
		items.push({name:'syubetu2', value:syubetu2});
		var syubetu3 = $('#syubetu3').attr('checked') ? '1' : '0';
		items.push({name:'syubetu3', value:syubetu3});
		var syubetu4 = $('#syubetu4').attr('checked') ? '1' : '0';
		items.push({name:'syubetu4', value:syubetu4});
		var syubetu5 = $('#syubetu5').attr('checked') ? '1' : '0';
		items.push({name:'syubetu5', value:syubetu5});

		items.push({name:'ekitoho', value:$('select[name=ekitoho]').find('option:selected').index()});
		var bas_riyou = $('#bas_riyou').attr('checked') ? '1' : '0';
		items.push({name:'bas_riyou', value:bas_riyou});

		//専有
		items.push({name:'senyu1', value:$('select[name=senyu1]').find('option:selected').index() + 1});
		items.push({name:'senyu2', value:$('select[name=senyu2]').find('option:selected').index() + 2});

		//間取り
		$('input[type=checkbox][name^=madori]').each(function(index){
			if($(this).attr('id') != 'madoriGroup'){
				var checked = $(this).attr('checked') ? '1' : '0';
				items.push({name:$(this).attr('id'), value:checked});
			}
		});
		var madoriGroupCheck = $('#madoriGroupCheck').attr('checked') ? '1' : '0';
		items.push({name:'madoriGroupCheck', value:madoriGroupCheck});

		var built = $('select[name=built]').find('option:selected').index();
		items.push({name:'built', value:built});

		//人気こだわり
		$('input[type=checkbox][name^=kodawari_]').each(function(){
			var check = $(this).attr('checked') ? '1' : '0';
			items.push({name:$(this).attr('name'), value:check});
		});

		//こだわり
		$('[name=kodawariGroup]').each(function(index){
			var realIndex = String(index + 1);
			var groupName = 'kodawariType_' + (realIndex.length > 1 ? realIndex : '0' + realIndex);
			var check = '0';
			if($(this).find('input[type=checkbox]:checked').length > 0){
				check = '1';
			}
			items.push({name:groupName, value:check});
		});

		//こだわり単独
		$('input[type=checkbox][name^=kodawari]').each(function(){
			if($(this).attr('name').indexOf('kodawari_') < 0){
				var check = $(this).attr('checked') ? '1' : '0';
				items.push({name:$(this).attr('name'), value:check});
			}
		});

		//キャンペイン
		var campaign = [];
		$('input[type=checkbox][name^=campaign]:checked').each(function(){
			var div = $(this).parent().first();
			var label = div.parent().first();
			var text = label.contents().filter(function() {
				return this.nodeType == 3;
			});
			campaign.push(text[text.length - 1].data);
		});
		items.push({name:'campaignName', value:campaign.join('/')});

		//console.dir(items);
		submit(href, 'POST', items);
	}


	function submit(action, method, values) {
 	   var form = $('<form/>', {
	        action: action,
	        method: method
	    });
	    $.each(values, function() {
	        form.append($('<input/>', {
	            type: 'hidden',
	            name: this.name,
	            value: this.value
	        }));
	    });
	    form.appendTo('body').submit();
	}

	/********************************************************
	 * 条件を問い合わせ
	 ********************************************************/
	function inquirySetting(){
		$navi.find('.btn_consult a').on('click', function(e){
			var href = $(this).attr('href');
			e.preventDefault();
			saveInquiryCondition(href);
		});
	}
	/*add 2014.12.02*/
	/*******************************************************
	 *　boxSection
	 *******************************************************/
	function boxSection(){
		$('.mod_box_sub_apaman .box_content .wrapper_shop .box_shop').each(function(i){
			var $self = $(this),
				link = $self.find('h3 a').attr('href'),
				$pic = $self.find('.pic'),
				$btn = $self.find('.mod_btn');

			$pic.on('click', function(e){
				location.href = link;
			});
			$btn.on('click', function(e){
				location.href = link;
			});
		});
	};
	/*
	----------------------------------------------------------*/

	saveSetting();
	inquirySetting();
	boxSection();
	mapModalStationSetting();
});
